package mb.model;

// import java.awt.Color;  // -
// import java.awt.Point;  // -
// import java.awt.Component;  // -
import java.awt.*;  // +
import java.awt.event.MouseListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;  // +
import javax.swing.JPanel;

public class Diagram extends JPanel
  implements MouseListener, MouseMotionListener {  // &

  public static final Color DIAGRAM_COLOR = Color.WHITE;

  private Point mousePressed = new Point(0, 0);  // +

  public Diagram() {
    super();
    this.addMouseListener(this);
    this.setBackground(DIAGRAM_COLOR);
    this.setLayout(null);
  }

  private void addNewBox(Point aPoint) {
    Box newBox = new Box();
    newBox.setLocation(aPoint);
    newBox.addMouseListener(this);
    newBox.addMouseMotionListener(this);  // +
    this.add(newBox);
    this.repaint();
  }

  public void selectAllBoxes() {
    Component[] components = this.getComponents();
    Box box;
    for (int i = 0; i < components.length; i++) {
      if (components[i] instanceof Box) {
        box = (Box) components[i];
        box.setSelected(true);
      }
    }
  }

  public void deSelectAllBoxes() {
    Component[] components = this.getComponents();
    Box box;
    for (int i = 0; i < components.length; i++) {
      if (components[i] instanceof Box) {
        box = (Box) components[i];
        box.setSelected(false);
      }
    }
  }

  public void hideSelectedBoxes() {
    Component[] components = this.getComponents();
    Box box;
    for (int i = 0; i < components.length; i++) {
      if (components[i] instanceof Box) {
        box = (Box) components[i];
        if (box.isSelected()) {
          box.setVisible(false);
        }
      }
    }
  }

  public void showSelectedBoxes() {
    Component[] components = this.getComponents();
    Box box;
    for (int i = 0; i < components.length; i++) {
      if (components[i] instanceof Box) {
        box = (Box) components[i];
        if (box.isSelected()) {
          box.setVisible(true);
        }
      }
    }
  }

  // {+
  public Dimension getPreferredSize() {
    return new Dimension(1600, 1600);
  }
  // +}

  public void mousePressed(MouseEvent e) {
    if (e.getSource() == this) {
      this.addNewBox(e.getPoint());
    }
    else if (e.getSource() instanceof Box) {
      Box clickedBox = (Box) (e.getSource());
      clickedBox.setSelected(!(clickedBox.isSelected()) );
      mousePressed = e.getPoint();  // +
    }
  }

  public void mouseClicked(MouseEvent e) {}
  public void mouseReleased(MouseEvent e) {}
  public void mouseEntered(MouseEvent e) {}
  public void mouseExited(MouseEvent e) {}

  // {+
  public void mouseDragged(MouseEvent e) {
    if (e.getSource() instanceof Box) {
      Box box = (Box) e.getSource();
      // box location (x, y) and size (r) before the box has been dragged
      Rectangle r = box.getBounds();
      int x = r.x + e.getX() - mousePressed.x;
      int y = r.y + e.getY() - mousePressed.y;
      if (x < 0) x = 0;
      if (y < 0) y = 0;
      box.setLocation(x, y);
    }
  }

  public void mouseMoved(MouseEvent e) {}
  // +}

}